package main

import (
	"fmt"
	"reflect"
)

// 结构体的标签，以及对标签的取值

func S12() {
	//	标签可用于解释字段
	type s1 struct {
		name  string    "名称"
		age   int16     "年龄"
		hobby [2]string "爱好"
	}

	var v1 s1 = s1{"myc", 18, [2]string{"BENZ", "BWM"}}

	var reflectV1 = reflect.TypeOf(v1)
	fmt.Println(reflectV1)

	//取值方式1
	var v1Tag = reflectV1.Field(0)
	fmt.Println(v1Tag.Tag, v1Tag.Name)

	//取值方式2
	var v1Tag1, err = reflectV1.FieldByName("name")
	fmt.Println(v1Tag1, err)

	//循环取值
	var v1Num = reflectV1.NumField() //取到s1中一共有多少个字段
	for i := 0; i < v1Num; i++ {
		var v1Tag = reflectV1.Field(i)
		fmt.Println(v1Tag.Name, v1Tag.Tag)
	}
}
